<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>授信信息列表</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<!-- 是否显示提交按钮 -->
<sys:funcAuth resCd="SplSubmit">
	<span id="isShowCmtBtn" ></span>
</sys:funcAuth>
<!-- 是否显示审核按钮 -->
<sys:funcAuth resCd="SplCheck">
	<span id="isShowAudBtn" ></span>
</sys:funcAuth>
<body class="iframeBody">
<div class="Wrapper">
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldset1 clearFix">
          <div class="fieldBox" >
                <div class="label">融资机构</div>
                <div class="field">
                 <input name="fncInsNmQ" id="fncInsNmQ" type="text" class="ui-textbox" value="${fncInsNm}">
                </div>
            </div> 
            <div class="fieldBox" style="display:none;">
                <div class="field">
                   <input name="fncInsIdQ" id="fncInsIdQ" type="text" class="ui-textbox" value="${fncInsId}">
                </div>
            </div>
			<div class="fieldBox">
                <div class="label">授信主体</div>
                <div class="field">
                    <select name="crSbjQ" id="crSbjQ" data-value=" ">
                    	<option value=" " selected="selected">请选择</option>
                         <c:forEach items="${crsbjList }" var="kv">
                     		<option value="${kv.key }">${kv.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>        
            <div class="fieldBox">
                <div class="label">授信名称</div>
                <div class="field">
                   <input name="crNmQ" id="crNmQ" type="text" class="ui-textbox" value="">
                </div>
            </div>    
            <div class="fieldBox">
                <div class="label">授信编号</div>
                <div class="field">
                   <input name="crIdQ" id="crIdQ" type="text" class="ui-textbox" value="">
                </div>
            </div> 
            
            <div class="fieldBox">
                <div class="label">授信时间</div>
                <div class="field">
                   <input name="stDtQ" id="stDtQ" type="text" class="ui-datepicker" value=""  disabled="disabled" >
                   <span>—</span>
                   <input name="edDtQ" id="edDtQ" type="text" class="ui-datepicker" value=""   disabled="disabled" >
                </div>
            </div>
           
            <div class="fieldBox">
                <div class="label">授信额度</div>
                <div class="field">
                   <input name="crLmtMinQ" id="crLmtMinQ" type="text" class="ui-textbox" value="">
                   <span>—</span>
                   <input name="crLmtMaxQ" id="crLmtMaxQ" type="text" class="ui-textbox" value="">
                </div>
            </div>
       
            <div class="fieldBox">
                <div class="label">授信余额</div>
                <div class="field">
                   <input name="crBalMinQ" id="crBalMinQ" type="text" class="ui-textbox" value="">
                   <span>—</span>
                   <input name="crBalMaxQ" id="crBalMaxQ" type="text" class="ui-textbox" value="">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">是否循环授信</div>
                <div class="field">
                    <select name="rvlCrQ" id="rvlCrQ" data-value=" ">
                       <option value=" " selected="selected">请选择</option>
                        <c:forEach items="${rvlcrList}" var="kv">
                     		<option value="${kv.key }">${kv.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">授信状态</div>
                <div class="field">
                    <select name="crStQ" id="crStQ" data-value=" ">
                       <option value=" " selected="selected">请选择</option>
                       <c:forEach items="${crstList}" var="kv">
                     		<option value="${kv.key }">${kv.value }</option>
                       </c:forEach>
                    </select>
                </div>
            </div>
             <div class="fieldBox">
                <div class="label">资产占用种类</div>
                <div class="field">
                    <select name="astIdQ" id="astIdQ" data-value=" ">
                       <option value=" " selected="selected">请选择</option>
                       <c:forEach items="${astidList}" var="kv">
                     		<option value="${kv.key }">${kv.value }</option>
                       </c:forEach>
                    </select>
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">授信是否已备案</div>
                <div class="field">
                    <select name="isBa" id="isBa" data-value=" ">
                       <option value="" selected="selected">请选择</option>
                       <option value="1" >是</option>
                       <option value="0" >否</option>
                       
                    </select>
                </div>
            </div>
            <div class="clearFix"></div>
            <div class="btnsBox formBtns notRight">
                <span id="searchBtn" class="sq-button-round">查询</span>
                <span id="clearBtn" class="sq-button-round">清空</span>
                <!-- <span id="addBtn" class="sq-button-round" onclick="add();">新增</span> -->
                <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
            </div>
          </div>
        </form>
    </div>
</div>
<div id="ligergrid1" class="sq-grid"></div>
<!-- 隐藏融资机构编号、名称、及缩写 -->

<script type="text/javascript">
	var grid = null;
	var fncInsId =$("#fncInsIdQ").val();
	if(fncInsId==null||fncInsId==""){
		 $("#fncInsNmQ").attr({"disabled":false});
	}else{
		$("#fncInsNmQ").attr({"disabled":true});
	}
	$(function () {
		var _url = '<%=path%>/rfbase/crbaseinfo/initList.do?fncInsIdQ='+fncInsId;
	    $("#ligergrid1").ligerGrid({
	    	url: _url,
	    	method : 'get',
	        columns: [
	            { display: '授信名称', name: 'crNm', align:'left', width: 150, 
	            	render: function (rowdata, rowindex, value){
	            		if(rowdata.crNm==null)
	            			rowdata.crNm="";
	            		var queryInfo ='<a href="javascript:showDetail(';
	            		queryInfo +="'"+rowdata.id+"'";
	            		queryInfo +=')">'+rowdata.crNm+'</a>';
	                	return queryInfo;
	                }
	            },
	            { display: '授信合同号', name: 'crCtrNo', width: 150},
	            { display: '授信状态', name: 'crStNm', width: 100 },
	            { display: '授信是否已备案', name: 'isBa', width: 100, render: function (rowdata, rowindex, value, column) { 
	            	var h='否';
	            	if(value=='1') h="是";
                    return h;
            		},
        	      enabledSort:true}, 
	            { display: '添加人', name: 'crtUsrNm', width: 100 },
	            { display: '授信主体', name: 'crSbjNm', width: 100 },
	            { display: '起始日期', name: 'stDt', width: 200, render:renderDate},
	            { display: '结束日期', name: 'edDt', width: 200, render:renderDate},
	            { display: '是否循环', name: 'rvlCrNm', width: 80},
	            { display: '关联授信', name: 'incCrNm', width: 80},
	            { display: '授信额度', name: 'crLmt', width: 150, render: function (rowdata, rowindex, value, column) { 
	            	var money =moneyChange(value);
                    return money;
            		},
        	      enabledSort:true}, 
	            { display: '授信余额', name: 'crBal', width: 150, render: function (rowdata, rowindex, value, column) { 
	            	var money = 0;
                	if(rowdata.crBal==null){
                		money = rowdata.crLmt;
            		}else
            			money = rowdata.crBal;
                	return moneyChange(value);
            		},
        	      enabledSort:true},  
	            { display: '操作', width: 280 , align:'left',
	           
	            	render: function (rowdata, rowindex, value){
	            		//查看
	            		var queryInfo ='<a class="sq-button-hasicon" href="javascript:showDetail(';
	            	    	queryInfo +="'"+rowdata.id+"'";
	            	    	queryInfo +=')"><i class="sqBtnIco_32"></i>查看</a>';
	            		//修改
	            		var updateInfo ='<a class="sq-button-hasicon" href="javascript:change(';
	            		    updateInfo +="'"+rowdata.id+"'";
	            		    updateInfo +=')"><i class="sqBtnIco_4"></i>修改</a>';
	            		//发起流程
	            			var flowInfo ='<a class="sq-button-hasicon" href="javascript:startFlow(';
	            			flowInfo +="'"+rowdata.id+"'";
	            			flowInfo +=')"><i class="sqBtnIco_47"></i>启用流程</a>';
	            		//删除
	            		var deleteInfo ='<a class="sq-button-hasicon" href="javascript:deleteRecord(';
	            		    deleteInfo +="'"+rowdata.crId+"'";
	            		    deleteInfo +=')"><i class="sqBtnIco_7"></i>删除</a>';
	            		
	            		//作废
		            	var cancelInfo ='<a class="sq-button-hasicon" href="javascript:cancelRecord(';
		            	    cancelInfo +="'"+rowdata.id+"'";
		            	    cancelInfo +=')"><i class="sqBtnIco_5"></i>作废</a>';
	            		
		            	//停用(授信状态为‘正常’可以停用)
			            var disableInfo ='<a class="sq-button-hasicon" href="javascript:disableRecord(';
			            	disableInfo +="'"+rowdata.id+"'";
			            	disableInfo +=')"><i class="sqBtnIco_14"></i>停用</a>';
	            		
		            	//启用(授信状态为‘已停用’，可以重新启用)
			            var enableInfo ='<a class="sq-button-hasicon" href="javascript:enableRecord(';
			            	enableInfo +="'"+rowdata.id+"'";
			            	enableInfo +=')"><i class="sqBtnIco_15"></i>启用</a>';
	            		   
	            		//CR_BSC_INFO_CR_ST_NOCR = 0;   授信未发起
	            		if(rowdata.crSt==0){
	            			return queryInfo +" " + updateInfo +" " + flowInfo + "  " + deleteInfo;
	            		}
	            		//CR_BSC_INFO_CR_ST_CRING = 2;授信中
	            		//CR_BSC_INFO_CR_ST_COVER = 5;授信覆盖
	            		//CR_BSC_INFO_CR_ST_COVER = 3;已停用
	            		//CR_BSC_INFO_CR_ST_NOPASS = 4;废弃
	            		if(rowdata.crSt==2||rowdata.crSt==5||rowdata.crSt==4){
	            			return queryInfo;
	            		}
	            		if(rowdata.crSt==3){//已停用
	            			return queryInfo + " " + enableInfo;
	            		}
	            		var ba="";
	            		<sys:funcAuth resCd="shouxin_ba">
	            		ba ='<a class="sq-button-hasicon" href="javascript:enableBa(';
		            	ba+="'"+rowdata.id+"'";
		            	ba+=')"><i class="sqBtnIco_15"></i>备案</a>';
	            	   </sys:funcAuth>
	            		
									
	            		if(rowdata.crSt==1){//正常
	            			if(rowdata.isBa=='1'){//已备案不用显示按钮
	            				return queryInfo + " " + cancelInfo + " " + disableInfo;
	            			}else{
	            				return queryInfo + " " + cancelInfo + " " + disableInfo+" " +ba;
	            			}
	            		}
	            		
	            	}
	            }
	        ],
	        pageSize:10, 
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        checkbox:false,
	        rownumbers:true,
	    });
	    grid = liger.get("ligergrid1");
	    grid.setHeight(100);
	    $("#searchBar").ligerForm();
	
	    $("#searchBtn").click(function(event) {
	         var form = new liger.get("searchBar");
	         var data = form.getData();
	         for (var name in data){
	            grid.setParm(name,data[name]);
	         };
	        grid.reload();
	    });
	    $("#clearBtn").click(function(event) {
	        $("#resetBtn").click();  //重置 input-text
	        var form = liger.get("searchBar");
	        form.setData({  //重置select等特殊表单
	            "crSbjQ": " ",
	            "crNmQ": " ",
	            "crIdQ": " ",       
	            "stDtQ": " ",
	            "edDtQ": " ",
	            "crLmtMinQ": " ",
	            "crLmtMaxQ": " ",
	            "crBalMinQ": " ",
	            "crBalMaxQ": " ",
	            "crStQ": " ",
	            "rvlCrQ": " ",
	            "astIdQ": " ",
	            "isBa":""
	        });
	        if(fncInsIdQ==null||fncInsIdQ==""){
		   		 $("#fncInsNmQ").val("");
		   	}
	    });
	});
	
	 //新增数据
	var callback = function () {
		grid.reload();
    };
    
	function add(){
		Add_navTab('PrdSpl_1'+fncInsId,'添加授信','<%=path%>/rfbase/crbaseinfo/toAddPage.do?fncInsId='+fncInsId);
		
	}
	
	//修改数据
	function change(id){
		var lgcId=id;
		Add_navTab('PrdSpl_2'+id,'修改授信','<%=path%>/rfbase/crbaseinfo/toAddPage.do?lgcId='+lgcId);
	};
	
	//详情
	function showDetail(id){
		if(!id)return;
		var lgcId=id;
		Add_navTab('PrdSpl_3'+id,'授信详情','<%=path%>/rfbase/crbaseinfo/toDetailPage.do?lgcId='+lgcId);
	}
	
	//废弃数据
	function cancelRecord(id){
		if(!id)return;
		$.ligerDialog.confirm('确定废弃该授信吗?', function (flag) {
			if(flag == false)return;
			var wait_dialog;
			$.ajax({
				type: 'get',
				dataType:'json',
				url: '<%=path%>/rfbase/crbaseinfo/cancelCrRecord.do?id='+id,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 grid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});
	}
	
	//删除数据
	function deleteRecord(crId){
		if(!crId)return;
		$.ligerDialog.confirm('确定删除吗?', function (flag) {
			if(flag == false)return;
			var wait_dialog;
			$.ajax({
				type: 'get',
				dataType:'json',
				url: '<%=path%>/rfbase/crbaseinfo/remove.do?crId='+crId,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 grid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});
	}
	
	//启用数据
    function enableRecord(id){
		$.ligerDialog.open({
            width: 700,
            height: 'auto',
            //isResize: true,
            showMax: true,
            title: '启用授信',
            url: '<%=path%>/rfbase/crbaseinfo/toEnableCrPage.do?flag=1&id='+id
		});
	}
  //授信备案
    function enableBa(id){

		if(!id)return;
		$.ligerDialog.confirm('确定备案该授信吗?', function (flag) {
			if(flag == false)return;
			var wait_dialog = null;
			$.ajax({
				type: 'get',
				dataType:'json',
				url: '<%=path%>/rfbase/crbaseinfo/switchCrRecord.do?flag=4&id='+id,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 grid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});
	}
	//停用数据
	function disableRecord(id){
		if(!id)return;
		$.ligerDialog.confirm('确定停用该授信吗?', function (flag) {
			if(flag == false)return;
			var wait_dialog = null;
			$.ajax({
				type: 'get',
				dataType:'json',
				url: '<%=path%>/rfbase/crbaseinfo/switchCrRecord.do?flag=3&id='+id,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 grid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});
	}
	
	function iframeResize () {
	    if (parent.mainApp) {
	        parent.mainApp.parentTab.onResize();
	    };
	}
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
		}
		return dateStr;
	}
	
	//发起审核流程
	function startFlow(_id){
		if(!_id)return;
		var modelTyp ="R001";//流程业务分类-再融资授信审核
	//	alert(modelTyp);
		if(StringUtils.isEmpty(modelTyp)){
			console.log("modelTyp is null");
			return;
		}
        var flowDialog = $.ligerDialog.SQopen({
            url: '<%=path%>/flow/flow/toForStartListPage.do?modelTyp='+modelTyp,
            width: 700,
            height:400,
            title : '请选择要发起的流程',
            buttons: [{
	                text: "确定", type: 'ok', cls: 'l-dialog-btn-ok',
	                onclick: function (item, Dialog) {
	                    if (Dialog.onOk) Dialog.onOk();
	                }
	            }, {
	                text: "取消", type: 'cancel', cls: 'l-dialog-btn-no',
	                onclick: function (item, Dialog) {
	                    if (Dialog.onCancel) Dialog.onCancel();
	                }
	            }
	        ],
            onClose: function () {
            	var json = flowDialog._DATA;
            	console.log(json);
            	if(!json){
                	return;
            	}
            	var wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting",onClose: function () {}});
        		$.ajax({
            		type: 'post',
            		dataType:'json',
            		data:{
            			id:_id,
            			procDefKey:json.procDefKey,
            			procDefId:json.procDefId,
            			procNo:json.procNo
            		},
            		url: '<%=path%>/rfbase/crbaseinfo/addFlowStart.do',
            		success:function(msg){
            			wait_dialog.close();
            			 if(msg.success == 1){
          					 UIUtils.alertSuccessHold1(msg.info);
           				 	 grid.reload();
	           			 }else{
	          				UIUtils.alertErrorHold1(msg.info);
	           			 }
            		}
            	});
            }
        });
	}	
	
//格式化金额
 function moneyChange(value) { 
		var h = '';
			if(value&&value!="null"){
		    h+=fmoney(value, 2);
			}else{
			h+="0.00";
			}
         return h;
		}
 
 //格式化金额
 function fmoney(s, n)   
	{   
	   n = n > 0 && n <= 20 ? n : 2;   
	   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
	   var l = s.split(".")[0].split("").reverse(),   
	   r = s.split(".")[1];   
	   t = "";   
	   for(i = 0; i < l.length; i ++ )   
	   {   
	      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
	   }   
	   return t.split("").reverse().join("") + "." + r;   
	} 
</script>
</body>
</html>